package com.floreo.shopping.model;

import java.util.*;

/**
 */
public class ShoppingList {

    private String name;
    private List<ShoppingItem> items = new ArrayList<ShoppingItem>();
    private Map<Group, List<ShoppingItem>> groupProduct = new HashMap<Group, List<ShoppingItem>>();


    public ShoppingList(String name) {
        this.name = name;
    }

    public void add(ShoppingItem item) {
        items.add(item);
        List<ShoppingItem> list = groupProduct.get(item.getProduct().getGroup());
        if(list == null) {
            list = new ArrayList<ShoppingItem>();
            list.add(item);
            groupProduct.put(item.getProduct().getGroup(), list);
        } else {
            list.add(item);
        }
    }

    public void remove(ShoppingItem item) {
        items.remove(item);
        groupProduct.get(item.getProduct().getGroup()).remove(item);
    }

    public int size() {
        return items.size();
    }

    public Iterator<ShoppingItem> getIterator() {
        return items.iterator();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ShoppingItem> get(Group group) {
        return groupProduct.get(group);
    }
}
